package com.shuyu.framework.web.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shuyu.common.core.domain.entity.Customer;
import com.shuyu.domain.CustomerWx;
import com.shuyu.service.CustomerService;
import com.shuyu.service.CustomerWxService;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class AuthLoginService {
    @Autowired
    private TokenService tokenService;
    @Resource
    protected WxMpService wxMpService;
    @Resource
    private CustomerWxService customerWxService;
    @Resource
    private CustomerService customerService;

    public String login(String code) throws WxErrorException {
        WxOAuth2AccessToken accessToken = this.wxMpService.getOAuth2Service().getAccessToken(code);
        String openId = accessToken.getOpenId();
        String unionId = accessToken.getUnionId();
        int count = customerWxService.count(new LambdaQueryWrapper<CustomerWx>().eq(CustomerWx::getUnionId, unionId));
//        说明用户已经通过微信快捷登录注册了
        if(count>0){
            WxMpUser wxMpUser = this.wxMpService.getUserService()
                    .userInfo(accessToken.getOpenId());
        }else {
            CustomerWx customerWx = customerWxService.getOne(new LambdaQueryWrapper<CustomerWx>().eq(CustomerWx::getUnionId, unionId).last("limit 1"));
            Customer customer = customerService.getOne(new LambdaQueryWrapper<Customer>().eq(Customer::getThirdId, customerWx.getId()).last("limit 1"));
        }

        return null;

//        final WxMaService wxService = WxMaConfiguration.getMaService();
//        try {
//            WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(code);
//            log.error("sessionKey:{}",session.getSessionKey());
//            log.error("openid:{}",session.getOpenid());
//            CustomerThird customerThird = customerThirdService.getOne(new LambdaQueryWrapper<CustomerThird>().eq(CustomerThird::getOpenId, session.getOpenid())
//                    .eq(CustomerThird::getRoleType,roleType).last("limit 1"));
//            String mobile = null;
//            if(customerThird==null){
//                CustomerThird memberInfo = new CustomerThird(session.getUnionid(), session.getOpenid(), new Date(),null,null,null,roleType);
//                customerThirdService.save(memberInfo);
//            }else{
//                Customer customer = customerService.getById(customerThird.getCustomerId());
//                if(customer != null){
//                    mobile = customer.getMobile();
//                }
//            }
//            WxMaJscode2SessionResultVo maJscode2SessionResultVo = new WxMaJscode2SessionResultVo();
//            if(session != null) {
//                maJscode2SessionResultVo.setOpenid(session.getOpenid());
//                maJscode2SessionResultVo.setSessionKey(session.getSessionKey());
//                maJscode2SessionResultVo.setUnionid(session.getUnionid());
//            }
//            maJscode2SessionResultVo.setMobile(mobile);
//            return Result.OK(maJscode2SessionResultVo);
//        } catch (Exception e) {
//            log.error(e.getMessage(), e);
//            return Result.error(e.toString());
//        }
    }
}
